package com.yya.ratelimiter.config;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * @Autor: yuanyongan
 * @Description:
 * @Date 2023/1/13 14:25
 */

@Configuration
@EnableScheduling
public class ScheduleConfig implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(taskScheduler());
    }

    /**
     * 通过newScheduledThreadPool线程池来实现多线程定时任务
     * @return
     */
    @Bean(name = "taskScheduler")
    public Executor taskScheduler(){
        ThreadFactory nameThreadFactory = new ThreadFactoryBuilder().setNamePrefix("scheduler-pool-%d").build();
        return Executors.newScheduledThreadPool(4, nameThreadFactory);
    }
}
